import java.util.Stack;

public class Sort {
    public static  void inserSort(int [] array){
        for(int i = 0; i< array.length;i++){
            int tmp = array[i];
            int j = i-1;
            for (; j >=0 ; j--) {
                if (array[j] > tmp) {
                    array[j+1] = array[j];
                }else {
                    //array[j+1]= tmp;
                    break;
                }
            }
            array[j+1]= tmp;
        }
    }


    public static void shellsort(int [] arrary){
        int gap = arrary.length;
        while (gap > 1){
            gap /= 2;
            shell(arrary,gap);
        }

    }
    private static void shell(int [] array,int gap){
        //i++交替进行插入排序
        for (int i = gap;i < array.length; i++){
            int tmp = array[i];
            int j = i-gap;
            for (; j >= 0 ; j -= gap) {
                if (array[j] > tmp){
                    array[j+gap] = array[j];
                }else {
                    break;
                }

            }
            array[j+gap] =tmp;
        }
    }
    private static void swap(int[] arrary,int i,int j){
        int tmp = arrary[i];
        arrary[i] = arrary[j];
        arrary[j] = tmp;

    }
    public static void selectSort1(int[] array){
        for (int i = 0; i < array.length; i++) {
            int minIndex = i;
            for (int j = i+1; j < array.length; j++) {
                if (array[j] < array[minIndex]) {
                    minIndex = j;
                }

            }
            swap(array,minIndex,i);

        }

    }
    public static void selectSort2(int[] array){
        int left = 0;
        int right = array.length -1;
        while (left < right){
            int mindIndex = left;
            int maxIndex = left;
            for (int i = left+1; i <= right; i++) {
                if (array[i] < array[mindIndex]) {
                    mindIndex = i;
                }
                if (array[i] > array[maxIndex]) {
                    maxIndex = i;
                }
            }
            swap(array,mindIndex,left);
            if (maxIndex == left) {
                maxIndex = mindIndex;
            }
            swap(array,maxIndex,right);
            left++;
            right--;
        }
    }
    public static void  bubbleSort(int[] arrary){
        for (int i = 0; i <arrary.length-1 ; i++) {
            for (int j = 0; j < arrary.length-1-i; j++) {
                if(arrary[j]> arrary[j+1]){
                    swap(arrary,j,j+1);
                }

            }
        }

    }
    public static void  quickSort(int[] arrary){
        quick(arrary,0,arrary.length-1);

    }
    private static void quick(int [] arrary,int start,int end){
        if (start >= end) {
            return;
        }
        if (end - start + 1 <= 10) {
            inserSortRange(arrary,start,end);
            return;
        }

        int index = midThreeNum(arrary,start,end);
        swap(arrary,index,start);

        int par = partitionWaken(arrary,start,end);
        quick(arrary,start,par-1);
        quick(arrary,par+1,end);
    }
    public static  void inserSortRange(int [] array,int left,int right){
        for(int i = left+1; i< right;i++){
            int tmp = array[i];
            int j = i-1;
            for (; j >=0 ; j--) {
                if (array[j] > tmp) {
                    array[j+1] = array[j];
                }else {
                    //array[j+1]= tmp;
                    break;
                }
            }
            array[j+1]= tmp;
        }
    }
    private static int partition(int [] arrary,int left,int right){
        int i = left;
        int tmp = arrary[left];
        while (left < right){
            //right-- : 先走左边会导致最后相遇的地方比基准大的数据,
            // 交换完后,会把大于基准的值换到前面
            while (left < right && arrary[right] >= tmp){
                right--;
            }
            while (left < right && arrary[left] <= tmp){
                left++;
            }
            swap(arrary,left,right);
        }
        //此时相遇left=right;
        swap(arrary,left,i);
        return right;
    }
    private static int partitionWaken(int [] arrary,int left,int right){
        int tmp = arrary[left];
        while (left<right){
            while (left < right && arrary[right] >= tmp){
                right--;
            }
            arrary[left] = arrary [right];
            while (left<right && arrary[left] <= tmp){
                left++;
            }
            arrary[right] = arrary[left];
        }
        arrary[left] = tmp;
        return left;
    }
    private static int midThreeNum(int [] arrary,int left,int right){
        int mid = (left+right)/2;
        if (arrary[left] < arrary[right]){
            if (arrary[mid] < arrary[left]) {
                return left;

            }else if (arrary[mid] > arrary[right]){
                return right;
            }else {
                return mid;
            }
        }else{
            if (arrary[mid] < arrary[right]){
                return right;
            }else if(arrary[mid] > arrary[left]){
                return left;
            }else {
                return mid;
            }
        }
    }
//非递归快速排序
    public static void quickNot(int[] array){
        Stack<Integer> stack = new Stack<>();
        int left = 0;
        int right = array.length - 1;
        int par = partition(array,left,right);
        if (par > left+1){
            stack.push(left);
            stack.push(par-1);

        }
        if (par < right-1){
            stack.push(par+1);
            stack.push(right);
        }
        while (!stack.isEmpty()){
            right = stack.pop();
            left = stack.pop();
            par = partitionWaken(array,left,right);
            if(par > left+1){
                stack.push(left);
                stack.push(par-1);
            }
            if (par < right -1){
                stack.push(par+1);
                stack.push(right);
            }    //归并排序

        }
    }
    //归并排序
    public static void mergeSort(int[] array) {
        mergeSortFun(array,0,array.length-1);
    }

    public static void mergeSortFun(int[] array,int left,int right) {
        if(left >= right) {// >
            return;
        }
        int mid = (right+left) / 2;
        mergeSortFun(array,left,mid);
        mergeSortFun(array,mid+1,right);
        //合并
        merge(array,left,mid,right);
    }
    private static void merge(int[] array,int left,
                              int mid,int right) {
        int[] tmp = new int[right-left+1];
        int k = 0;
        int s1 = left;
        int e1 = mid;
        int s2 = mid+1;
        int e2 = right;
        while (s1 <= e1 && s2 <= e2) {
            if(array[s1] <= array[s2]) {
                tmp[k++] = array[s1++];
            }else {
                tmp[k++] = array[s2++];
            }
        }
        while (s1 <= e1) {
            tmp[k++] = array[s1++];
        }
        while (s2 <= e2) {
            tmp[k++] = array[s2++];
        }
        //走到这里 相当于tmp数组 当中 所有的元素 都有序了
        //接下来把tmp数组的内容 拷贝到array数组当中
        for (int i = 0; i < k; i++) {
            array[i+left] = tmp[i];
        }
    }
    //非递归归并排序
    public static void mergeSortNor(int[] array) {
        int gap = 1;
        while (gap < array.length) {
            for (int i = 0; i < array.length; i = i + 2*gap) {
                int left = i;
                int mid = left+gap - 1;
                if(mid >= array.length) {
                    mid = array.length-1;
                }
                int right = mid+gap;
                if(right >= array.length) {
                    right = array.length-1;
                }
                merge(array,left,mid,right);
            }
            gap *= 2;
        }
    }
    //计数排序
    public static void countSort(int[] array) {
        //1. 遍历数组 求最大值 和 最小值
        int maxVal = array[0];
        int minVal = array[0];
        for (int i = 0; i < array.length; i++) {
            if(maxVal < array[i]) {
                maxVal = array[i];
            }
            if(minVal > array[i]) {
                minVal = array[i];
            }
        }
        //2. 定义count数组
        int[] count = new int[maxVal - minVal + 1];
        //3. 遍历array数组 把值 放入 计数数组当中
        for (int i = 0; i < array.length; i++) {
            int val = array[i];//98
            count[val - minVal]++;
        }
        //4. 以上3步完成之后，计数数组 已经存好了对应的数据
        // 接下来 开始遍历 计数数组
        int index = 0;//array的下标
        for (int i = 0; i < count.length; i++) {
            while (count[i] > 0) {
                array[index] = i+minVal;
                index++;
                count[i]--;
            }
        }
    }


}
